package cn.tianyigps.obd.beans;


import cn.tianyigps.obd.utils.MessageCoding;
import cn.tianyigps.obd.utils.StrConvertUtil;
import io.netty.buffer.ByteBuf;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

/**
 * Created with IDEA
 *
 * @description: OBD设备 起始位 7E
 * @author: cui.guibin
 * @create: 2019/3/7 11:16
 **/
public class OBD extends Equipment{

    static Logger log = LogManager.getLogger(OBD.class);
    public OBD() {
    }

    public OBD(String equipmentId, String type) {
        super(equipmentId, type);
    }

    @Override
    public boolean checkData(String data) {
        return false;
    }

    @Override
    public List<StandardModel> analysisPackageType(Object msg) {
        byte[] bytes = MessageCoding.getBytes((ByteBuf) msg);
        //获取标识位置
        String transData = MessageCoding.bytesToHexString(bytes).toUpperCase();
        log.info("转译前数据 {}", transData);
        if (transData.contains("7D01"))
            transData = transData.replace("7D01","7D");
        if (transData.contains("7D02"))
            transData = transData.replace("7D02","7E");
        if (!transData.endsWith("7E"))
            transData = transData + "7E";
        log.info("转译后数据 {}", transData);
        String packageType = MessageCoding.bytesToHexString(new byte[]{bytes[0], bytes[1], bytes[2]});
        List<StandardModel> standardModels = StrConvertUtil.analysisByM5BStr(transData, "111", packageType, this.getEquipmentId());
        return standardModels;
    }


    @Override
    public byte[] response(String strData) {

        byte[] response = new byte[0];
        //注册应答包
        if (strData.toUpperCase().startsWith("7E0100")) {
            StringBuffer stringBuffer = new StringBuffer("8100000D");
            stringBuffer.append(strData.substring(10, 26));
            stringBuffer.append("00010001020304050607080910Z17E");
            //7e设备获取校验码要去掉消息头”7e“
            log.info("回复的包为：" + "7E" + MessageCoding.bytesToHexString(MessageCoding.byteToCheck(MessageCoding.hexStringToBytes(stringBuffer.toString()))));
            response =  MessageCoding.hexStringToBytes("7E" + MessageCoding.bytesToHexString(MessageCoding.byteToCheck(MessageCoding.hexStringToBytes(stringBuffer.toString()))));
        } else {
//            Equipment equipment = pipeline.getEquipment();
//            if (strData.toUpperCase().startsWith("7E0200")) {
//                //收到有重量值的定位包下发显示屏指令
//                List<StandardModel> standardModels = StrConvertUtil.analysisByM5BStr(strData, null, packageType, equipment.getEquipmentId());
//                log.info("netWeight 重量值为："+standardModels.get(0).toString());
//                if (null != standardModels.get(0).getOther() && !"".equals(standardModels.get(0).getOther())) {
//                    Gson gson = new Gson();
//
//                    JsonObject jsonObject = gson.toJsonTree(standardModels.get(0)).getAsJsonObject();
//                    Double netWeight = jsonObject.get("netWeight").getAsDouble();
//
////                    Object netWeight = gson.parseObject(standardModels.get(0).getOther()).get("netWeight");
//                    log.info("netWeight 重量值为："+netWeight);
//                    if (null != netWeight && !"".equals(netWeight) && standardModels.size() == 1) {
//                        try {
//                            Double d = Double.parseDouble(netWeight.toString());
//                            Integer i = d.intValue();
//                            String showCmd = CmdPojo.getCmd(new CmdParamPojo(this.message.getEquId(), "show", String.format("%08d", i), null, null, null));
//                            log.info("showCmd : " + showCmd + " , netWeight :　" + i  + " ,下发的显示屏指令为 ： " + showCmd);
//                            ctx.writeAndFlush(Unpooled.copiedBuffer(messageCoding.hexStringToBytes(showCmd)));
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
        }

        return response;
    }

    @Override
    public byte[] generalResponse(String strData) {
        //终端通用应答0001   使用通用回复
        StringBuffer stringBuffer = new StringBuffer("80010005");
        stringBuffer.append(strData.substring(10, 26));
        stringBuffer.append(strData.substring(22, 26));
        stringBuffer.append(strData.substring(2, 6));
        stringBuffer.append("00Z17E");
        log.info("回复的包为：" + "7E" + MessageCoding.bytesToHexString(MessageCoding.byteToCheck(MessageCoding.hexStringToBytes(stringBuffer.toString()))));
        byte[] bytes = MessageCoding.hexStringToBytes("7E" + MessageCoding.bytesToHexString(MessageCoding.byteToCheck(MessageCoding.hexStringToBytes(stringBuffer.toString()))));
        return bytes;
    }

    @Override
    public byte[] command(String data) {
        // 获取设备值守指令，返回下发
        return new byte[0];
    }

    @Override
    public List<StandardModel> analysisHeartPackage(String data) {
        return null;
    }

    @Override
    public List<StandardModel> analysisLocationPackage(String data) {
        return null;
    }

    @Override
    public List<StandardModel> analysisLoginPackage(String data) {
        return null;
    }

    @Override
    public List<StandardModel> analysisAlarmPackage(String data) {
        return null;
    }

    @Override
    public List<StandardModel> analysisReplyPackage(String data) {
        return null;
    }
}
